package com.fafa.swing.base;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author : fafa
 * @version V1.0
 * @Project: SwingTest
 * @Package com.fafa.swing
 * @Description: ActionListDemo
 * @date Date : 2019年11月24日 17:27
 */
public class ActionListDemo extends JFrame {

    JList list;
    JLabel label;
    JButton button;
    int clicks = 0;
    public ActionListDemo(){
        setTitle("动作事件监听器");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(300,300,400,200);

        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5,5,5,5));
        contentPane.setLayout(new BorderLayout(0,0));
        setContentPane(contentPane);
        JLabel label = new JLabel("     ");
        label.setFont(new Font("楷体",Font.BOLD,16));
        contentPane.add(label,BorderLayout.SOUTH);
        JButton button = new JButton("我是普通按钮");
        button.setFont(new Font("楷体",Font.BOLD,16));

        /*button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText("按钮被单击了 "+(clicks++)+" 次！");
            }
        });*/
        button.addActionListener(new button1ActionListener(label));

        contentPane.add(button);

    }

    // 处理按钮单击事件的匿名内部类
    class button1ActionListener implements ActionListener{

        private JLabel label;

        public button1ActionListener(JLabel label){
            this.label = label;
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            label.setText("按钮被单击了 "+(clicks++)+" 次！");
            JOptionPane.showConfirmDialog(null,"错误的测试！","错误",0);
        }
    }

    public static void main(String[] args) {
        ActionListDemo actionListDemo = new ActionListDemo();
        actionListDemo.setVisible(true);
    }
}
